#include "CGameManager.h"
#include "CImage.h"
#include "CText.h"

CCGameManager * CCGameManager::singleton = 0;

CCGameManager::CCGameManager(void)
{
	//std::cout << "SDL initialized." << std::endl;
	Log_Debug("SDL initialized.\n");

	const int SCREEN_WIDTH = 1200;// 0 means use current width.
	const int SCREEN_HEIGHT = 900;// 0 means use current height.
	const int SCREEN_BPP = 32;// 0 means use current bpp.
	const Uint32 SCREEN_FLAGS = SDL_SWSURFACE;
	this->pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SCREEN_FLAGS);
	if (this->pScreen == 0){
		std::string str = "SDL_SetVideoMode faild!\n";
		throw str.append(SDL_GetError());
	}
	//SDL_WM_SetCaption("SDL_DEMO", 0);
	this->loadObj();
}

CCGameManager::~CCGameManager(void)
{
}

CCGameManager * CCGameManager::Instance()
{
	if (singleton == 0)
	{
		singleton = new CCGameManager();
	}
	return singleton;
}

void CCGameManager::render()
{
	for (std::list<CCObject*>::iterator iter = this->objList.begin(); iter != this->objList.end(); ++iter)
	{
		if (SDL_BlitSurface(((CCObject*)*iter)->drawable(), 0, this->pScreen, ((CCObject*)*iter)->drawRect()) != 0){
			std::string log = "SDL_BlitSurface faild!\n";
			throw log.append(SDL_GetError());
		}
	}
	 SDL_Flip(this->pScreen);
}

void CCGameManager::loadObj()
{
	CCObject *bgObj = new CCImage("Res\\background.png");
	this->objList.push_back(bgObj);
	CCObject *fgObj = new CCImage("Res\\foreground.png");
	fgObj->setRect(0, 600, 1200, 300);
	this->objList.push_back(fgObj);
	CCObject *paopao = new CCImage("Res\\dapaopao.png");
	this->objList.push_back(paopao);
	CCText *text = new CCText("hello world");
	text->setTextFont("times.ttf");
	text->setTextRGB(0xff, 0, 0);
	this->objList.push_back(text);
}
